package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.*;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.api.service.ApiT_member_partner_taskService;
import cn.com.api.service.ApiU_orgService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/**
 * 
 * ClassName: ApiMemberPartnerController
 * 
 * @Description: 合伙人模块接口
 * @author yewl
 * @date 2016年12月29日
 */
@Controller
@RequestMapping(Utils.SYS_API + "partner/")
public class ApiMemberPartnerController {
	@Autowired
	private ApiT_memberService memberService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiU_orgService apiU_orgService;
	@Autowired
	private ApiT_member_partnerService apiT_member_partnerService;
	@Autowired
	private ApiT_member_partner_taskService apiT_member_partner_taskService;
	private static Logger logger = Logger.getLogger(ApiMemberPartnerController.class);

	/**
	 * 获取开户机构信息的接口
	 * 
	 * @param corp_code
	 *            机构代码
	 * @param user_id
	 *            用户id
	 * @param partner_code
	 *            推荐人认证码，如果不为空，则查询该推荐人所属机构
	 * @param token
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getOrgList")
	public void getOrgList(HttpServletRequest request, HttpServletResponse response, String corp_code,
			String partner_code, String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (partner_code != null && !"".equals(partner_code)) {
				map.put("partner_code", partner_code);
			}
			if (corp_code != null && !"".equals(corp_code)) {
				map.put("corp_code", corp_code);
			}
			// List<ApiU_org> list = apiU_orgService.findU_orgs();
			List<ApiU_org> list = apiU_orgService.findU_orgsByMap(map);
			map.clear();
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据地址获取响应的开户机构
	 * 
	 * @param token
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getOrgInfoByAddress")
	public void getOrgInfoByAddress(HttpServletRequest request, HttpServletResponse response, String province,
			String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (province != null && !"".equals(province)) {
				map.put("address", province);
			}
			List<ApiU_org> list = apiU_orgService.findU_orgsByMap(map);
			map.clear();
			if (list != null && list.size() > 0) {
				map.put(Utils.DATAS, list.get(0));
			} else {
				ApiU_org apiU_org = apiU_orgService.getRandomOrg();
				map.put(Utils.DATAS, apiU_org);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取推荐人的信息
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param partner_code
	 *            推荐人的认证编号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getPartnerInfo")
	public void getPartnerInfo(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String partner_code) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("partner_code", partner_code);
			List<ApiT_member> list = memberService.findT_membersByMap(map);
			map.clear();
			if (list != null && list.size() > 0) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list.get(0));
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
				map.put(Utils.MESSAGE, "用户不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	/**
	 * 获取合伙人任务信息的接口
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param partner_code
	 *            推荐人的认证编号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getPartnerTasks")
	public void getPartnerTasks(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String task_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			// if (checkService.checkToken(user_id, token, map) &&
			// checkService.checkParams(paramMap, map)) {
			// map.clear();
			if (task_id != null && !"".equals(task_id)) {
				map.put("task_id", task_id);
			}
			List<ApiT_member_partner_task> list = apiT_member_partner_taskService.findMemberPartnerTasksByMap(map);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, list);
			// }
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 申请成为合伙人
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "applyPartner")
	public void applyPartner(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiT_member_partner partner) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiT_member_partnerService.applyPartner(partner);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));

	}

	/**
	 * 获取申请成为合伙人的申请信息 包括订单信息
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param partner
	 *            接收申请合伙人信息资料的实体类，接口参数名为实体属性名即可
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getPartner")
	public void getPartner(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiT_member_partnerService.getPartner(Integer.valueOf(user_id));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	/**
	 * @Title: getGoldPartnersInfo
	 * @Description: 获取金牌合伙人申请的情况,
	 * @param request
	 * @param response
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "getGoldPartnersInfo")
	public void getGoldPartnersInfo(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String partnerid) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				// 获取金牌合伙人剩余人数，排队人数信息
				Map<String, Object> result = apiT_member_partnerService.getGoldPartnersInfo(partnerid);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	@Transactional
	@RequestMapping(value = "testPartnerQueue")
	public void testPartnerQueue(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Integer ret = this.apiT_member_partnerService.assignNoRefereeEntre();
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.DATAS, ret);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

}
